package com.btb.adapter.mapper;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class IpWhiteListDao {
	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	final static String LIST_SQL = "SELECT ip FROM `ip_white_list`";

	public List<String> list() {
		return namedParameterJdbcTemplate.queryForList(LIST_SQL, new MapSqlParameterSource(), String.class);
	}

	public boolean check(String userIp) {
		List<String> list = list();
		if (Objects.isNull(list)) {
			return true;
		}
		return list.stream().map(StringUtils::trim).filter(StringUtils::isNotEmpty).anyMatch(x -> userIp.equals(x));
	}
}
